package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.HeartActivityPartMapper;
import com.ruoyi.community.service.IBbsPunishService;
import com.ruoyi.community.service.IHeartActivityService;
import com.ruoyi.community.service.impl.BbsPunishServiceImpl;
import com.ruoyi.community.utils.MessageUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/heart")
public class HeartActivityController extends BaseController {
    @Autowired
    private IHeartActivityService heartActivityService;
    @Autowired
    private HeartActivityPartMapper heartActivityPartMapper;
    @Autowired
    private IBbsPunishService bbsPunishService;
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list")
    public TableDataInfo list(HeartActivityDetail heartActivityDetail){
        startPage();
        heartActivityDetail.setCommunityid(SecurityUtils.getCommunityId());
        List<HeartActivityDetail> list = heartActivityService.selectHeartActivityDetailList(heartActivityDetail);
        return getDataTable(list);
    }
    /**
     *   微信家有老小首页 只能看到 statusflag = 0 或 1的
     * */
    @GetMapping("/wxlist")
    public TableDataInfo wxlist(HeartActivityDetail heartActivityDetail){
        startPage();
        heartActivityDetail.setCommunityid(SecurityUtils.getCommunityId());
        List<HeartActivityDetail> list = heartActivityService.selectHeartActivityDetailWxList(heartActivityDetail);
        return getDataTable(list);
    }

    @GetMapping("/myPartActivity")
    public TableDataInfo getMylist(){
        startPage();
        HeartActivityDetail temp = new HeartActivityDetail();
        temp.setParticipatorId(SecurityUtils.getUserID());
        List<HeartActivityDetail> list = heartActivityService.selectHeartActivityDetailList(temp);
        return getDataTable(list);
    }

    @GetMapping("/activity/{activityId}")
    public HeartActivityDetail getActivity(@PathVariable Long activityId){
        return heartActivityService.selectHeartActivityDetailById(activityId);
    }
//    @PutMapping()
//    public AjaxResult update(@RequestBody BbsArticleDetail bbsArticleDetail){
//        return toAjax(bbsArticleService.updateByArticleId(bbsArticleDetail));
//    }
    @PutMapping("/activity/update")
    public int updateActivity(@RequestBody HeartActivityDetail heartActivityDetail){
        return heartActivityService.updateByActivityId(heartActivityDetail);
    }

    @PostMapping("/selectList")
    public TableDataInfo getList(@RequestBody HeartActivityDetail heartActivityDetail){
        startPage();
        List<HeartActivityDetail> list = heartActivityService.selectHeartActivityDetailList(heartActivityDetail);
        return getDataTable(list);
    }

    @PostMapping("/activity/insert")
    public AjaxResult insert(@RequestBody HeartActivityDetail heartActivityDetail){
        heartActivityDetail.setInitiatorid(SecurityUtils.getUserID());
        heartActivityDetail.setCommunityid(SecurityUtils.getCommunityId());
        int res =  heartActivityService.insert(heartActivityDetail);
        if(res==1){
            return AjaxResult.success();
        }
        else{
            return AjaxResult.error("插入失败");
        }
    }
    @GetMapping("/activity/part/{activityid}")
    public HeartActivityPart getPart(@PathVariable Long activityid){
        // 只有一个activityid字段查询 另外一个是隐藏的StatusFlag = 0 即正在参与的用户
        return heartActivityPartMapper.selectByActivityid(activityid);
    }
    @PostMapping("/activity/participate")
    public AjaxResult participate(@RequestBody HeartActivityPart heartActivityPart){
        Long activityid = heartActivityPart.getActivityid();

        heartActivityPart.setParticipatorid(SecurityUtils.getUserID());
        HeartActivityPart searchActivity = new HeartActivityPart();
        searchActivity.setActivityid(activityid);
        searchActivity.setStatusflag(0);
        List<HeartActivityPart> res= heartActivityPartMapper.selectHeartActivityPartsList(searchActivity);
        if(res.size()>0){
            return AjaxResult.error(300,"有人参与了");
        }else{
            int ans = heartActivityPartMapper.insert(heartActivityPart);
            if(ans == 1){
                HeartActivityDetail temp = new HeartActivityDetail();
                temp.setActivityid(activityid);
                temp.setStatusflag(1); // 活动详情设为有人参加
                heartActivityService.updateByActivityId(temp);

                //活动通知
                Remind remind = new Remind();
                remind.setSenderId(SecurityUtils.getUserID());
                String username = sysUserService.returnNameByUserid(SecurityUtils.getUserID());
                remind.setSenderName(username);
                remind.setSenderAction(RemindAction.HUODONG);
                remind.setUrl("/pages/elderly/activityDetail/activityDetail?activityId=" + activityid);
                remind.setRecipientId(heartActivityService.getInitiatorid(activityid));
                remind.setContent(username + "参与了您发起的活动。");
                remind.setContentType("参与活动");
                MessageUtil.addRemind(remind);

                return AjaxResult.success();
            }
            return AjaxResult.error("参与失败");
        }
    }

    /**
     *   获取activity的所有参与者情况，包括已经退出的
     *  */
    @GetMapping("/myAllPart/{activityid}")
    public TableDataInfo getMyAllPart(@PathVariable Long activityid){
        startPage();
        HeartActivityPart temp = new HeartActivityPart();
        temp.setActivityid(activityid);
        List<HeartActivityPart> list = heartActivityPartMapper.selectHeartActivityPartsList(temp);
        return getDataTable(list);
    }
    @PostMapping("/remotePart")
    public int removePart(@RequestBody HeartActivityPart heartActivityPart){
        int res =  heartActivityPartMapper.refusePartByInitiator(heartActivityPart);
        if(res == 1){
            //活动通知
            Remind remind = new Remind();
            remind.setSenderId(SecurityUtils.getUserID());
            String username = sysUserService.returnNameByUserid(SecurityUtils.getUserID());
            remind.setSenderName(username);
            remind.setSenderAction(RemindAction.HUODONG);
            remind.setUrl("/pages/elderly/activityDetail/activityDetail?activityId=" + heartActivityPart.getActivityid());
            remind.setRecipientId(heartActivityPart.getParticipatorid());
            remind.setContent(username + "拒绝了您的活动参与。");
            remind.setContentType("拒绝活动参与者");
            MessageUtil.addRemind(remind);
        }
        return res;
    }
    /**
     *   活动发起者决定完成活动
     *  */
    @PostMapping("/activity/finish")
    public int finish(@RequestBody HeartActivityDetail res){
        return heartActivityService.updateByActivityId(res);
    }
    /**
     *      在
     *      参与者退出活动 dePart  statusflag = 1
     *      活动者结束活动 finish  statusflag = 2
     *      活动者拒绝该参与者deletePart 活动者中止活动 terminate  statusflag = 3
     *      时，会调用updateNowPart
     * */
    @PostMapping("/activity/updataNowPart")
    public int updateNowPart(@RequestBody HeartActivityPart heartActivityPart){
        int res =  heartActivityPartMapper.updateByPrimaryKeySelective(heartActivityPart);
        Long activityid = heartActivityPart.getActivityid();
        if(res == 1){
            int flag = heartActivityPart.getStatusflag();
            Remind remind = new Remind();
            remind.setUrl("/pages/elderly/activityDetail/activityDetail?activityId=" + activityid);
            remind.setSenderAction(RemindAction.HUODONG);
            if(flag==1){
                Long senderid = SecurityUtils.getUserID();
                String name = sysUserService.returnNameByUserid(senderid);
                remind.setSenderId(senderid);
                remind.setSenderName(name);
                remind.setRecipientId(heartActivityService.getInitiatorid(activityid));
                remind.setContentType("参与者退出活动");
                remind.setContent(name + "退出了您发布的活动。");
            }
            if(flag == 2 || flag == 3){
                Long senderid = heartActivityService.getInitiatorid(activityid);
                String name = sysUserService.returnNameByUserid(senderid);
                remind.setSenderId(senderid);
                remind.setSenderName(name);
                remind.setRecipientId(heartActivityPart.getParticipatorid());
                if(flag == 2){
                    remind.setContentType("活动完成");
                    remind.setContent("恭喜您完成" + name + "发布的活动。");
                } else {
                    remind.setContentType("活动中止");
                    remind.setContent( name + "作为活动发起者终止了活动。");
                }
            }
            MessageUtil.addRemind(remind);
        }
        return res;
    }
}
